



(function(){
    function Control(map) {
        this.foods = [];
        this.body = [];
        this.direction = 'right';
        this.map = map;
        that = this;
    };
    var that;
    var Maxlevel = 3;
    var Currentlevel = 0;
    Control.prototype.Start = function () {
        // 生成食物
        for (var i = 0; i < Maxlevel; i++) {
            var food = new Unit({});
            var maxX = map.offsetWidth / food.width;
            var maxY = map.offsetHeight / food.height;
            var x = Tools.getRandom(0, maxX) - 1;
            var y = Tools.getRandom(0, maxY) - 1;
            food.render(map, x, y);
            this.foods.push(food);
        }
        // 生成蛇
        // 蛇頭
        var head = new Unit({ x: 10, y: 10, color: "red" });
        head.render(map);
        this.body.push(head);
        // 蛇尾
        var tail1 = new Unit({ x: 9, y: 10, color: "blue" });
        tail1.render(map);
        this.body.push(tail1);
        var tail2 = new Unit({ x: 8, y: 10, color: "purple" });
        tail2.render(map);
        this.body.push(tail2);
        document.onkeydown = function (e) {
            switch (e.key) {
                case ('ArrowUp'):
                    if (that.direction !== 'down')
                        that.direction = 'up';
                    break;
                case ('ArrowDown'):
                    if (that.direction !== 'up')
                        that.direction = 'down';
                    break;
                case ('ArrowRight'):
                    if (that.direction !== 'left')
                        that.direction = 'right';
                    break;
                case ('ArrowLeft'):
                    if (that.direction !== 'right')
                        that.direction = 'left';
                    break;
            }
        };
    }
    Control.prototype.Move = function () {
        var that = this;
        var timer = setInterval(function () {
            var head = that.body[0];
            var maxX = map.offsetWidth / head.width - 1;
            var maxY = map.offsetHeight / head.height - 1;
            var x = head.x;
            var y = head.y;
            switch (that.direction) {
                case 'right': head.x++;
                    break;
                case 'left': head.x--;
                    break;
                case 'up': head.y--;
                    break;
                case 'down': head.y++;
            }
            if (head.x > maxX || head.x < 0 || head.y > maxY || head.y < 0) {
                alert("lose");
                clearInterval(timer);
                return;
            }
            for (var i = that.body.length - 1; i > 1; i--) {
                that.body[i].x = that.body[i - 1].x;
                that.body[i].y = that.body[i - 1].y;
            }
            that.body[1].x = x;
            that.body[1].y = y;
            // 判斷是否吃到食物
            for (var i = 0; i < that.foods.length; i++) {
                var food = that.foods[i];
                if (head.x === food.x && head.y === food.y) {
                    that.foods.splice(i, 1);
                    that.body.push(food);
                    Currentlevel++;
                    break;
                }
            }
            that.repaint();
            if (Currentlevel === Maxlevel) {
                alert("win");
                clearInterval(timer);
                return;
            }
        }, 100, that);
    }
    Control.prototype.repaint = function () {
        for (var i = 0; i < this.body.length; i++)
            this.body[i].repaint();
    }
    window.Control=Control;
})();
